<script type="text/javascript" src="<?php echo URL::base(); ?>media/js/jquery.min.js"></script>
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<style type="text/css">
div#pageContent {
}
#provides{width:85%;}
.form-horizontal .control-label{width:110px;}
.form-horizontal .controls{ margin-left:115px;}
#gongying tr th{ color:#333333; font-weight:normal;}
</style>

<script type="text/javascript">
$(document).ready(function(){
    $.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });
    var strs= new Array();
    var str = "<?php echo date('Y-m-d',time());?>";
    strs=str.split("-");	
    $("#sdate").datepicker({
        minDate: new Date(strs[0], strs[1]-1, strs[2]),
    });  
    $("#skuss").bind('keydown', function (e) {
        var key = e.which;
        if(key==13) {
            click_sku();
        }
    });
    $("#sku_find").click(function(){
        click_sku();
    });
});

function click_sku() {
    var na=$("#skuss").val();
    if(na.length>10) {
        alert("sku长度大于10");return ;
    }
    if(na=="") {
        alert("请输入SKU");
        return ;
    }
    var list;
    var i=0;
    $.ajax({
        type:'post',
        url:'<?php echo URL::base(true); ?>purchase/selectsupplier',
        dataType:'json',
        data:{'sku':na},
        async:false,
        success:function(data) {
            var table='';
            list = data;
            if(!data) {
                alert("产品无供应商或者无此SKU");return ; 	
            }
            $("#s_name").val("");
            $.each(data,function(key,value){
                if(key%10 == 0) {
                    table +='<tr>';
                    i=i+1;
                    table += '<th>'+i+'</th>';
                }else if(key%10 == 1){
                    table += '<th id="s_name'+i+'">'+value+'</th>';
                }else if(key%10==4){
                    //$("#supplier_id").val(value);
                    table += '<th id="s_id'+i+'" style="display:none">'+value+'</th>';
                    table += '</tr>';
                } else if((key%10 == 6)&&key<10){
                    $("#sku").val(value);
                } else if((key%10 == 7)&&key<10){
                    $("#sku_name").val(value);
                } else if((key%10 == 8)&&key<10){
                    $("#sku_unit").val(value);
                } else if((key%10 == 9)&&key<10){
                    $("#sdate").val(value);
                }else if(key%10 == 2){
                    table += '<th id="price'+i+'">'+value+'</th>';
                    table+='<th><input type="button" class="btn btn-info" id="'+i+'" value="选择" /></th>';
                    table += '</tr>';
                }
            });
            $("#gongying").html(table);
        },

        error:function(){

        }
    });
    //添加一个建议价格查询
    $.ajax({
        type:'post',
        url:'<?php echo URL::base(true); ?>purchase/selectadvice',
        dataType:'json',
        data:{'sku':na},
        async:false,
        success:function(data) {
            $("#baseprice").css('display','block');
            $("#advice").html(data);
        },
        error:function(){

        }
    });
    $("#sele_type").change(function(){
        $("#pay_type").val($("#sele_type").val());
    });	
    $("table#provides input").click(function(){
        var cid = $(this).attr('id');
        //alert(cid);  // 1
        $("#sku_price").val($("#price"+$(this).attr('id')).text());
        $("#supplier_id").val($("#s_id"+$(this).attr('id')).text());
        $("#s_name").val($("#s_name"+$(this).attr('id')).text());
        $("#sname").show();
        $("#s_date").show();
        var num = (cid-1)*10 + 3;
        var selectCode = document.getElementById("sele_type");
        for(var i=0;i<$("#sele_type option").length;i++) { 
                selectCode.options[i].removeAttribute("selected");
        }
        $("#sele_type option[value="+list[num]+ "]").attr('selected',"selected");
        if($("#sele_type").val()==0) {
            $("#sele_type").attr("disabled",false);
        } else {
            $("#sele_type").attr("disabled",true);
            $("#pay_type").val($("#sele_type").val());
        }
        var num2=(cid-1)*10 + 4;
    });
}
//计算总价格。
function atotle_price(){//alert("string");return ;
    var number=Number(document.getElementById("sku_number").value);
    var freight=document.getElementById("freight").value;
    var noneprices=document.getElementById("sku_price").value;
    var flag  = isInteger(noneprices);
    if(!flag) {
        alert("单价不是数字");return ;
    }
    var totles=(Number(number*noneprices)+Number(freight)).toFixed(2);
    if(number=="" || freight==""){
        document.getElementById("totle_ids").value=0;
    }else{
        document.getElementById("totle_ids").value=totles;
    }
}
//提示，当文本框为空时，不能进行提交。
function checkform(){
    $sku_number=document.getElementById("sku_number").value;  //sku
    $freight=document.getElementById("freight").value;
    $paytype=document.getElementById("sele_type").value;
    $tax_rate=isNum(document.getElementById("tax_rate").value);
    $sku=document.getElementById("sku").value;
    $supplier_id=document.getElementById("supplier_id").value;
    $noneprices=document.getElementById("sku_price").value;
    $s_name=document.getElementById("s_name").value;
    if(!isInteger($freight)) {
        alert("运费不是数字！");
        return false;
    }
    if(!isInteger(document.getElementById("tax_rate").value)) {
        alert("税率不是数字！");
        return false;
    }
    if($sku=='') {
        alert("sku不能为空！");
        return false;
    }
    if($supplier_id=='') {
        alert("未选择供应商！");
        return false;		
    }
    if($s_name=='') {
        alert("未选择供应商！");
        return false;		
    }
    if(!$tax_rate) {
        alert("税率超出范围(0~100)");
        return false;
    }
    if($freight=='') {
        alert("运费不能为空！");
        return false;
    }
    if(!isInteger($noneprices)) {
        alert("单价不是数字");return false;
    }
    if($paytype==0) {
        alert("请选择付款类型");
        return false;
    }
    if($sku_number=='') {
        alert("sku数量不能为空！");
        return false;
    }
    return true;
}

function isNum(num) {
    if((parseInt(num)>100)||(parseInt(num)<0)) {
        return false;
    }	
    return true;
}
//判断是否是数字
function isInteger(val) {
    return isNaN(val) ? false : true;
}
function clearNoNum(obj){   
    obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符  
    obj.value = obj.value.replace(/^\./g,"");  //验证第一个字符是数字而不是. 
    obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.   
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
} 
</script>

<body id="pageContent" class="well">
    <div class="container">
          <div class="controls">
                <h3 style="display:inline-block; margin:0; margin-right:5px; font-weight:normal;">sku:</h3>
                <input style="margin-top:-5px;" type="text"  class="input-xlarge" id="skuss" name="iproductID" >
                <div class="control-group" style="display: inline-block;">
                    <input type="button" class="btn btn-info" style=" margin-left:15px;margin-top: -5px;" id="sku_find" value="查 询" />
                </div>
                <div id="baseprice" style="float:right;margin-right: 421px;font-weight:bold;margin-top: 20px; width: 150px; display:none">
                    <h4 style="display:inline-block; margin:0; margin-right:5px; font-weight:normal;">系统成本价:</h4>
                    <lable id="advice"><lable>
                </div>
                <p class="help-block"></p>
          </div>

        <div class="row">
            <div class="span6">
                <form class="form-horizontal"  action="<?php echo URL::base(); ?>purchase/createpurchase" method="post" onsubmit="return checkform()">
                    <fieldset>

                    <div id="legend" class="">
                        <legend>新建采购单</legend>
                    </div>
                    <!-- -------------------------------------------------------------------------- -->   
                    <!-- 设置值传给控制器. -->          
                    <input type="hidden" name="supplier_id" id="supplier_id" value=""/>
                    <input type="hidden" name="providersname" id="provider_id_hidden"/>
                    <!-- -------------------------------------------------------------------------- -->
                    <div style="display:none" class="control-group">
                        <label class="control-label" for="input01">单据号码</label>
                        <div class="controls">
                            <input type="text" class="input-xlarge"  name="purchaseid" id="" value="<?php if(isset($purchase_array))echo $purchase_array;?>"/>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="input01" >SKU:</label>
                        <div class="controls">
                            <input type="text" class="input-xlarge"  name="sku" id="sku" value="" readonly="true"/>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="input01">中文名称:	</label>
                        <div class="controls">
                            <input type="text" class="input-xlarge"  name="sku_name" id="sku_name" value="" readonly="true"/>
                        </div>
                    </div>
                    <div style="display:none" class="control-group">
                        <label class="control-label" for="input01">单位</label>
                        <div class="controls">
                            <input type="text" class="input-xlarge"  name="sku_unit" id="sku_unit" value=""/>
                        </div>
                    </div>
                    <div style="display:none" id="sname" class="control-group">
                        <label class="control-label" for="input01">供应商名称:</label>
                        <div class="controls">
                            <input type="text" class="input-xlarge"  name="s_name" id="s_name" value="" readonly="true"/>
                        </div>
                    </div>
                    <div style="display:none" id="s_date" class="control-group">
                        <label class="control-label" for="input01">交货日期:</label>
                        <div class="controls">
                            <input type="text" class="input-xlarge"  name="sdate" id="sdate" value="" readonly="readonly"/>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="input01">计税方式:</label>
                        <div class="controls">
                            <select class="input-xlarge"  name="tax_way" id="tax_way" style="width: 284px">
                                <option value="0">不计税</option>	
                                <option value="-1">内含税</option>
                                <option value="1">外加税</option>
                            </select>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="input01">税率:</label>
                        <div class="controls">
                            <input type="text"  name="tax_rate" onkeyup="clearNoNum(this)" id="tax_rate" value="0" />%
                        </div>
                    </div>    
                    <div class="control-group">
                        <!-- Select Basic -->
                        <label class="control-label">货币种类:</label>
                        <div class="controls">
                            <select class="input-xlarge" name="moneytype" style="width: 284px">
                                <option>人民币</option>
                                <option>美金</option>
                            </select>
                        </div>
                    </div>
                    <div class="control-group">
                        <!-- Text input-->
                        <label class="control-label" for="input01">运费:</label>
                        <div class="controls">
                            <input type="text"   placeholder="" onkeyup="clearNoNum(this)" class="input-xlarge" id="freight" name="Freight" value="0" />
                        </div>
                    </div>
                    <div class="control-group" style="display:none">
                        <label class="control-label" for="input01">付款方式</label>
                        <div class="controls">
                            <input type="text"  name="pay_type" id="pay_type" value="" />
                        </div>
                    </div> 
                    <div class="control-group">
                        <label class="control-label">付款方式:</label>
                        <div class="controls">
                            <select class="input-xlarge"  name="paytype" id="sele_type" style="width: 284px" >
                                <option value="0">选择付款方式</option>
                                <?php
                                foreach ($pay_ways as $pay_way){?>
                                <option value="<?php echo $pay_way['id']?>"><?php echo $pay_way['pay_way']?></option>
                                <?php }?>
                            </select>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="input01" >数量:</label>
                        <div class="controls">
                            <input type="text"  class="input-xlarge" id="sku_number" name="sku_num"   onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" onBlur="atotle_price()"/>
                        </div>
                    </div>

                    <div class="control-group">
                        <!-- Text input-->
                        <label class="control-label" for="input01" >单价:</label>
                        <div class="controls">
                            <input type="text" name="sku_price" onkeyup="clearNoNum(this)" id="sku_price" class="input-xlarge" value="0" name="sku_price" onBlur="atotle_price()">
                            <!--<p class="help-block"></p>-->
                        </div>
                    </div>

                    <div class="control-group">
                        <!-- Text input-->
                        <label class="control-label" for="input01">总计:</label>
                        <div class="controls">
                            <input type="text" class="input-xlarge"  name="totle_num" id="totle_ids" readonly="readonly"  value="0" onmousedown="atotle_price()" />
                        </div>
                    </div>

                    <div class="control-group">
                        <!-- Textarea -->
                        <label class="control-label">备注:</label>
                        <div class="controls">
                            <div class="textarea">
                                <textarea type="" class="" style="margin: 0px; width: 272px; height: 80px;resize:none;" name="remarks" > </textarea>
                            </div>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label"></label>

                        <!-- Button -->
                        <div class="controls">
                            <button class="btn btn-info" id="addsubnit_ids">提交</button>
                        </div>
                    </div>

                </fieldset>
                </form>
            </div>
            <div class="span6">
                <table id="provides" class="table table-hover">
                    <thead>
                        <tr style=" background-color:#4EA392;color:#fff;">
                            <th>编号</th>
                            <th id="gongyingshang">供应商名称</th>
                            <th>单价</th>
                            <th>选择操作</th>
                        </tr>
                    </thead>
                    <tbody id="gongying">
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>